*******************************************************************************
* > Loop over subgroups
*******************************************************************************

foreach subgroup in All Democrat Republican {

*******************************************************************************
* >> Loop over datasets
*******************************************************************************

foreach dataset in gallup gss {

*******************************************************************************
* Load data
*******************************************************************************

use dataset_`dataset', clear
	
*******************************************************************************
* Keep the subgroup
*******************************************************************************

keep if subgroup == "`subgroup'"

*******************************************************************************
* Set params based on data set
*******************************************************************************

if "`dataset'" == "gallup" {
	rename conf Y
	local mytitle2 "Confidence (Gallup)"
	local treated_target "police"
	local yaxis 0.2(0.2)0.8
	local xaxis 1994(5)2014
}
if "`dataset'" == "gss" {
	rename nat Y
	local mytitle2 "Spending (GSS)"
	local treated_target "crimy"
	local yaxis -0.2(0.2)0.8
	local xaxis 1984(10)2014
}

*******************************************************************************
* Label lines if it's the first plot
*******************************************************************************

local legend1 legend(off)
local legend2 legend(off)
local mytext
if "`subgroup'" == "All" & "`dataset'" == "gallup" {
	local legend1 legend(order(1 "{fontface monospace:{bf:Actual}}" ///
		2 "{fontface monospace:{bf:Synthetic}}") ///
		symysize(0) symxsize(0) ring(0) pos(11) cols(1) ///
		region(lwid(none)))
	local legend2 legend(order(1 "{fontface monospace:{bf:Difference}}") ///
		symysize(0) symxsize(0) ring(0) pos(11) cols(1) ///
		region(lwid(none)))
	local mytext text(0.4 2010.5 "{&larr}Placebo BLM")
	
}

*******************************************************************************
* Set placebo year
*******************************************************************************

local placeboyear 2004
keep if year < 2014

*******************************************************************************
* Make lags
*******************************************************************************

su year
local mymin = r(min)

local lags
levelsof year if year < `placeboyear', local(levels)

foreach i of local levels {
	egen Y`i'm = mean(Y) if year == `i', by(target)
	egen Y`i' = mean(Y`i'm), by(target)
	drop Y`i'm
	local lags `lags' Y`i'
}

*******************************************************************************
* Set panel and get the treated ID
*******************************************************************************

encode target, gen(id)
xtset id year
su id if target == "`treated_target'"

*******************************************************************************
* Synth
*******************************************************************************

synth Y `lags', trunit(`r(mean)') trperiod(`placeboyear') keep(tmp, replace)
use tmp, clear

*******************************************************************************
* Graph
*******************************************************************************

#delimit;

gr tw
	(line _Y_treated _time, col(black) lwid(thick))
	(line _Y_synthetic _time, col(gray) lwid(thick))
	,
		name(g`dataset', replace)
		xline(`placeboyear', lpat(dash))
		ylab(`yaxis', angle(horiz))
		plotregion(style(none))
		ytitle("")
		xtitle("")
		`legend1'	
		xlab(1990(8)2022)
		title(`mytitle2')
		`mytext'
		;

#delimit cr

gr_edit .legend.plotregion1.label[2].style.editstyle color(gray) editcopy


local mytitle `subgroup'
if "`subgroup'" != "All" local mytitle = "`subgroup's" 

*******************************************************************************
* >> Close dataset loop
*******************************************************************************

}

*******************************************************************************
* Combine plots for each dataset
*******************************************************************************

local mytitle `subgroup'
if "`subgroup'" != "All" local mytitle = "`subgroup's" 

gr combine ggallup ggss, name(G`subgroup', replace) title(`mytitle')

*******************************************************************************
* > Close subgroup loop
*******************************************************************************

}

*******************************************************************************
* Combine plots for each subgroup
*******************************************************************************

gr combine GAll GDemocrat GRepublican, rows(3) ysize(4) xsize(3) ///
	imargin(zero)

*******************************************************************************
* Save
*******************************************************************************

gr export "figure_sc_placebos_time.pdf", replace

*******************************************************************************
* End
*******************************************************************************